package com.kouxin.appaccount.developer;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.kouxin.appaccount.trade.Trade;
import com.kouxin.appaccount.trade.TradeDao;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DeveloperAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = -5775910224731623987L;
	
	private Developer developer;
	private DeveloperDao developerDao;
	private Map<String, Object> session;
	
	private String confirmPassword;
	
	private TradeDao tradeDao;
	private List<Trade> trades;
	
	public String register() throws Exception {
		Developer example = new Developer();
		example.setUsername(developer.getUsername());
		if (!developerDao.findByExample(example).isEmpty()) {
			this.addFieldError("developer.username", "该用户名已经注册过 ");
			return INPUT;
		}
		developerDao.insert(developer);
		return SUCCESS;
	}
	
	public String login() throws Exception {
		Developer developer = developerDao.login(this.developer);
		if (developer != null) {
			session.put(DeveloperAuthenticationInterceptor.DEVELOPER_SESSION_KEY, developer);
			return SUCCESS;
		} else {
			this.addActionError("你的用户名和密码不符，请再试一次");
			return INPUT;
		}
	}
	
	@SuppressWarnings("unchecked")
	public String applist() throws Exception {
		session = (Map<String, Object>) ActionContext.getContext().get(ActionContext.SESSION);
        developer = (Developer) session.get(DeveloperAuthenticationInterceptor.DEVELOPER_SESSION_KEY);
        developerDao.loadSoftwares(developer);
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String soldapp() throws Exception {
		session = (Map<String, Object>) ActionContext.getContext().get(ActionContext.SESSION);
		developer = (Developer) session.get(DeveloperAuthenticationInterceptor.DEVELOPER_SESSION_KEY);
		Trade trade = new Trade();
		trade.setDeveloper(developer);
		trades = tradeDao.findByExample(trade);
		return SUCCESS;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public void setDeveloperDao(DeveloperDao dao) {
		this.developerDao = dao;
	}

	public Developer getDeveloper() {
		return developer;
	}

	public void setDeveloper(Developer developer) {
		this.developer = developer;
	}
	
	public TradeDao getTradeDao() {
		return tradeDao;
	}

	public void setTradeDao(TradeDao tradeDao) {
		this.tradeDao = tradeDao;
	}

	public List<Trade> getTrades() {
		return trades;
	}

	public void setTrades(List<Trade> trades) {
		this.trades = trades;
	}
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
}
